using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataTransferObject;
using DataAccess;


namespace DataAccess
{
    public class clsTapChiDAO:clsDataAccessHelper
    {

        // Fields
        private String tenTable;

        // Contructors
        public clsTapChiDAO()
        {
            this.tenTable = "TAPCHI";
        }
        #region Methods
        //-------------------------------------Lấy dữ liệu bảng Tạp Chí-----------------------

        public DataTable SelectAll()
        {
            String sql;
            DataTable dt;
            try
            {
                sql = "SELECT * FROM " + this.tenTable;
                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //-----------------------------------------------------------------------------//

        //-------------------------------------Thêm vào bảng Tạp Chí-----------------------

        public bool Insert(clsTapChiDTO tapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "INSERT INTO " + this.tenTable;
                sql += " values('";
                sql += tapchi.MaTapChi + "',N'";
                sql += tapchi.TenTapChi + "','";
                sql += tapchi.TenKy + "','";
                sql += tapchi.ThoiGian + "',N'";
                sql += tapchi.NgonNgu + "','";
                sql += tapchi.NgayNhap + "',N'";
                sql += tapchi.TrangThai + "')";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        //-----------------------------------------------------------------------------//

        //-------------------------------------Xóa vào bảng Tạp Chí-----------------------
        public bool Delete(clsTapChiDTO tapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "DELETE ";
                sql += " FROM " + this.tenTable;
                sql += " WHERE MATAPCHI LIKE '" + tapchi.MaTapChi + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }
        //-----------------------------------------------------------------------------//

        //-------------------------------------Sửa vào bảng Tạp Chí-----------------------

        public bool Update(clsTapChiDTO tapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "UPDATE " + this.tenTable;
                sql += " SET";
                sql += " TENTAPCHI = N'";
                sql += tapchi.TenTapChi + "',";
                sql += " TENKY = N'";
                sql += tapchi.TenKy + "',";
                sql += " THOIGIAN = '";
                sql += tapchi.ThoiGian + "',";
                sql += " NGONNGU = N'";
                sql += tapchi.NgonNgu + "',";
                sql += " NGAYNHAP = '";
                sql += tapchi.NgayNhap.Day + "/" + tapchi.NgayNhap.Month +"/"+ tapchi.NgayNhap.Year + "',";
                sql += " TRANGTHAI = '";
                sql += tapchi.TrangThai + "'";
                sql += " WHERE MATAPCHI LIKE '";
                sql += tapchi.MaTapChi + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        public bool UpdateStatus(clsTapChiDTO tapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "UPDATE " + this.tenTable;
                sql += " SET";
                sql += " TRANGTHAI = N'";
                sql += tapchi.TrangThai + "'";
                sql += " WHERE MATAPCHI LIKE '";
                sql += tapchi.MaTapChi + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }


        //-----------------------------------------------------------------------------//

        //Tìm tạp chí.
        public clsTapChiDTO TimTapChi(clsTapChiDTO obj)
        {
            String sql;
            DataTable dt;
            clsTapChiDTO TapChiDTO;
            DataRow Row;
            Object[] objRow;

            try
            {
                sql = "SELECT MATAPCHI,TENTAPCHI";
                sql += " FROM " + this.tenTable;
                sql += " WHERE MATAPCHI LIKE '" + obj.MaTapChi + "'";
                sql += " AND TRANGTHAI LIKE N'Chưa mượn'";
                dt = this.ExecuteQuery(sql);

                TapChiDTO = new clsTapChiDTO();
                if (dt.Rows.Count > 0)
                {
                    objRow = new Object[2];
                    objRow = dt.Rows[0].ItemArray;

                    TapChiDTO.MaTapChi = objRow[0].ToString();
                    TapChiDTO.TenTapChi = objRow[1].ToString();
                }

                return TapChiDTO;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}
